package com.elitel.frame.base.component.quartz;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * created by guoyanfei on 2018/7/6
 * 初始化数据源
 */
@Configuration
public class DruidPrimaryDataSourceConfig {
    @Value("${spring.db.url}")
    private String url;

    @Value("${spring.db.username}")
    private String username;

    @Value("${spring.db.password}")
    private String password;

    @Value("${spring.db.driver-class-name}")
    private String driverClassName;

    @Value("${spring.db.initial-size}")
    private Integer initialSize;

    @Value("${spring.db.min-idle}")
    private Integer minIdle;

    @Value("${spring.db.max-wait}")
    private Integer maxWait;

    @Value("${spring.db.time-between-eviction-runs-millis}")
    private Integer timeBetweenEvictionRunsMillis;

    @Value("${spring.db.validation-query}")
    private String validationQuery;

    @Value("${spring.db.test-while-idle}")
    private Boolean testWhileIdle;

    @Value("${spring.db.test-on-borrow}")
    private Boolean testOnBorrow;

    @Bean
    public DataSource primaryDataSource (){
        DruidDataSource datasource = new DruidDataSource();
        /* 基础配置 */
        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        /* 其他配置 */
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        // 最大连接数
        datasource.setMaxActive(30);

        return datasource;
    }

}
